<%--
  Created by IntelliJ IDEA.
  User: 27716
  Date: 2024/10/28
  Time: 19:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>链表</title>
    <script src="../../static/js/jquery-1.8.2.min.js"></script>
    <link rel="stylesheet" href="../../static/css/css.css">
    <script>
        $(function (){
            //1.取值 2.组装对象 3.ajax
            let goodsName = $("#goodsName").val();
            let obj = {goodsName:goodsName}
            getGoodsList(obj);
        })

        function getGoodsList(obj){
            $.ajax({
                url:"/goods/getGoodsList",
                type:"post",
                data:{reqInfos:JSON.stringify(obj)},
                dataType:"json",
                success(res){
                    console.log(res);
                    //1.取值 数据 上一页，下一页，尾页
                    if(res ==400){
                        alert("参数不正确");
                    }
                    let arr = res.list;
                    $("#pageNum").html(res.pageNum)
                    $("#pages").html(res.size)
                    $("#pageSize").html(res.pages)
                    $("#total").html(res.total)
                    $("#table").empty();
                    $("#table").append(`
                    <tr>
                        <td>
                            <input type="checkbox" id="check">
                        </td>
                        <td>商品编号id</td>
                        <td>商品编码</td>
                        <td>商品名称</td>
                        <td>库存数量</td>
                        <td>采购价格</td>
                        <td>备注</td>
                        <td>商品名</td>
                        <td>供应商名</td>
                        <td>操作</td>
                    </tr>
                    `)
                    for (let o of arr) {
                        $("#table").append(`
                        <tr>
                                <td>
                                    <input type="checkbox" class="ck" value="\${o.goodsId}">
                                </td>
                                <td>\${o.goodsId}</td>
                                <td>\${o.goodsCode}</td>
                                <td>\${o.goodsName}</td>
                                <td>\${o.inventoryQuantity}</td>
                                <td>\${o.purchasingPrice}</td>
                                <td>\${o.remarks}</td>
                                <td>\${o.goodsTypeName}</td>
                                <td>\${o.supplierName}</td>
                                <td>
                                    <input type="button" value="删除" onclick="getToDel(\${o.goodsId})">
                                    <input type="button" value="修改" onclick="getGoodsUpd(\${o.goodsId})">
                                </td>
                            </tr>
                        `)
                    }
                    //循环结束之后 追加按钮
                    $("#table").append(`
                        <tr>
                            <td colspan="100">

                                <input type="button" value="首页" onclick="doPage(1)">
                                <input type="button" value="上一页" onclick="doPage(\${res.prePage})">
                                <input type="button" value="下一页" onclick="doPage(\${res.nextPage})">
                                <input type="button" value="尾页" onclick="doPage(\${res.pages})">
                                页数搜索： <input type="text" id="pagesNum">
                                 <input type="button" value="搜索" onclick="getPage()">
                            </td>
                        </tr>
                    `)
                },
                error(){
                    alert("服务器故障")
                }
            })
        }
    </script>
</head>
<body>
名称：<input type="button" onclick="add()" value="添加">
<input type="text" id="goodsName">
<input type="button" value="搜索" id="search">
<table id="table">
    <tr>
        <td>
            <input type="checkbox" id="check">
        </td>
        <td>商品编号id</td>
        <td>商品编码</td>
        <td>商品名称</td>
        <td>库存数量</td>
        <td>采购价格</td>
        <td>备注</td>
        <td>商品名</td>
        <td>供应商名</td>
        <td>操作</td>
    </tr>
</table>
总记录条数 <span id="pageSize"></span>
当前第 <span id="pageNum"></span>页
当前<span id="pages"></span>页
共 <span id="total"></span>页
</body>
<script>
    //全选 全不选
    $(document).on('click',"#check",function (){
        $(".ck").prop("checked",this.checked);
    })
    //删除
    function getToDel(goodsId){
        let obj = {goodsUid:goodsId};
        $.ajax({
            url:"/goods/getToDel",
            type:"post",
            data: {reqInfos:JSON.stringify(obj)},
            dataType: "json",
            success(res) {
                if(res===200){
                    alert("删除成功");
                    location.reload();
                }
            },
            error() {
                alert("数据无法查看")
            }
        })
    }
    function getPage(){
        let pageNum = $("#pagesNum").val();
        if(pageNum<0){
            alert("数据不能负数")
            return;
        }
        doPage(pageNum)
    }
    //搜索框
    $("#search").click(function (){
        let goodsName = $("#goodsName").val();
        if(pagesNum!=null){
            alert("数据不能为空");
            return;
        }
        let obj = {goodsName:goodsName}
        getGoodsList(obj);
    })
    //搜索框
    function doPage(pageNum){
        let goodsName = $("#goodsName").val();
        let obj = {goodsName:goodsName,pageNum:pageNum}
        getGoodsList(obj);
    }
    //添加
    function add(){
        location.href = "add.jsp";
    }
    //修改
    function  getGoodsUpd(goodsId){
        location.href = "upd.jsp?goodsId="+goodsId;
    }
</script>
</html>
